<template>
	<view class="addressList">
		
		<!-- 地址列表 -->
		<view class="list flex-r-a-c" 
		v-for="(item,index) in userAddress" 
		:key="index" 
		:class=" activeUserId == item.id ? 'active' : '' "
		:data-userid="item.id"
		@tap.stop="active">
			<view class="list_l">
				<view class="list_l_t flex-r-s-c">
					<view class="name">{{item.name}}</view>
					<view class="tel">{{item.mobile}}</view>
				</view>
				<view class="address one-line-ellipsis">{{item.province}}{{item.address}}</view>
			</view>
			<image src="../../static/mine/textIcon.png" mode="widthFix" :data-userid="item.id" @tap.stop="editAddress"></image>
		</view>
		
		<!-- 新增地址按钮 -->
		<!-- <view class="addBtn" @tap="goAddress">新增收货地址</view> -->
		<view style="background-color:#FFFFFF;" class="nextStep">
			<view class="nextStepBtn" @tap="goAddress">新增收货地址</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				activeUserId: null, // 选择的用户地址id
				userAddress: [], // 用户地址列表
				page: 1 , // 页码
			};
		},
		onLoad(options){
			if(options.id){
				this.activeUserId = options.id
			}
		},
		onShow() {
			this.page = 1
			this.userAddress = []
			this.getUserAddressList()  // 获取用户地址列表
		},
		methods:{
			// 获取用户地址列表
			getUserAddressList(){
				let that = this
				that.$api.userAddress_list({page: that.page}).then(res => {
					that.userAddress = that.userAddress.concat(res.data.data)
					res.data.data.forEach(e => {
						if(e.default == 1){
							that.activeUserId = e.id
						}
					})
				})
			},
			// 点击选择用户地址
			active(e){
				let that = this
				that.activeUserId = e.currentTarget.dataset.userid
				let page = getCurrentPages()
				let pages = page[page.length - 2]
				if(pages.route == 'order/integralConfirm/integralConfirm'){
					pages.$vm.replaceAddress(that.activeUserId)
					uni.navigateBack()
				} else if(pages.route == 'order/orderConfirm/orderConfirm'){
					pages.$vm.getDefaultAddress({type:1, id: that.activeUserId})
					uni.navigateBack()
				}
				
			},
			// 点击跳转编辑用户地址
			editAddress(e){
				uni.navigateTo({
					url: '/mine/address/address?id=' + e.currentTarget.dataset.userid
				})
			},
			// 点击新增收获地址
			goAddress(){
				uni.navigateTo({
					url: '/mine/address/address'
				})
			}
		}
	}
</script>

<style lang="less">
	.addressList{
		width: 90vw;
		margin: 40rpx auto;
		box-sizing: border-box;
		padding-bottom: 100rpx;
		
		.active{
			border: 1px solid #45806E;
			box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.4);
		}
		
		.addBtn{
			width: 90%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background-color: #45806E;
			border-radius: 80rpx;
			font-size: 28rpx;
			font-weight: 500;
			color: #FFFFFF;
			position: fixed;
			bottom: 30rpx;
			left: 50%;
			transform: translateX(-50%);
		}
		
		.list{
			background-color: #FFFFFF;
			border-radius: 30rpx;
			width: 100%;
			height: 170rpx;
			box-sizing: border-box;
			padding: 30rpx;
			margin: 20rpx auto;
						
			.list_l{
				width: 80%;
				.list_l_t{
					font-size: 32rpx;
					color: #4B4B4B;
					.name{
						font-weight: bold;
					}
					.tel{
						margin-left: 40rpx;
						font-weight: 500;
					}
				}
				.address{
					margin-top: 20rpx;
					font-size: 28rpx;
					font-weight: 500;
					color: #767676;
				}
			}
			
			image{
				width: 40rpx;
				height: 40rpx;
			}
			
		}
	}
</style>
